package com.chaitin.xray.form;

import com.chaitin.xray.model.Poc;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.StyleContext;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class AllPoCForm {
    public JPanel searchPoC;
    private JLabel pocLabel;
    private JTextArea pocList;
    private JScrollPane pocScroll;
    private JTextField searchText;
    private JButton searchButton;
    private JPanel searchPanel;

    private void initLang() {
        if (MainForm.LANG == MainForm.CHINESE) {
            pocLabel.setText("相关的PoC");
            searchButton.setText("搜索");
            searchPoC.setBorder(BorderFactory.createTitledBorder(null, "PoC列表",
                    TitledBorder.DEFAULT_JUSTIFICATION,
                    TitledBorder.DEFAULT_POSITION, null, null));
        } else {
            pocLabel.setText("All PoC");
            searchButton.setText("Search");
            searchPoC.setBorder(BorderFactory.createTitledBorder(null, "PoC List",
                    TitledBorder.DEFAULT_JUSTIFICATION,
                    TitledBorder.DEFAULT_POSITION, null, null));
        }
    }

    public AllPoCForm() {
        initLang();
        pocList.setText(Poc.getString());
        pocList.setCaretPosition(0);
        searchButton.addActionListener(e -> {
            String search = searchText.getText();
            List<String> result = new ArrayList<>();
            for (String d : Poc.getPocList()) {
                if (d.trim().contains(search.toLowerCase().trim())) {
                    result.add(d);
                }
            }
            StringBuilder sb = new StringBuilder();

            Collections.sort(result);

            for (String r : result) {
                sb.append(r);
                sb.append("\n");
            }
            pocList.setText(sb.toString());
            pocList.setCaretPosition(0);
        });
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        searchPoC = new JPanel();
        searchPoC.setLayout(new GridLayoutManager(3, 1, new Insets(0, 0, 0, 0), -1, -1));
        searchPoC.setBackground(new Color(-12828863));
        searchPoC.setBorder(BorderFactory.createTitledBorder(null, "PoC列表", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        pocLabel = new JLabel();
        pocLabel.setText("相关的PoC");
        searchPoC.add(pocLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        pocScroll = new JScrollPane();
        pocScroll.setBackground(new Color(-12828863));
        searchPoC.add(pocScroll, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, new Dimension(500, 500), new Dimension(500, 500), new Dimension(500, 500), 0, false));
        pocList = new JTextArea();
        pocList.setBackground(new Color(-12828863));
        pocList.setEditable(false);
        Font pocListFont = this.$$$getFont$$$(null, -1, -1, pocList.getFont());
        if (pocListFont != null) pocList.setFont(pocListFont);
        pocList.setForeground(new Color(-1));
        pocScroll.setViewportView(pocList);
        searchPanel = new JPanel();
        searchPanel.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        searchPanel.setBackground(new Color(-12828863));
        searchPoC.add(searchPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        searchText = new JTextField();
        searchPanel.add(searchText, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        searchButton = new JButton();
        searchButton.setText("搜索");
        searchPanel.add(searchButton, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null) {
            resultName = currentFont.getName();
        } else {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
                resultName = fontName;
            } else {
                resultName = currentFont.getName();
            }
        }
        Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
        boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac");
        Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize());
        return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return searchPoC;
    }

}
